﻿using System;
using System.Web.Services;
using SimplePMT.Components.SvnLog;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using SimplePMT.Data;
using NHibernate.Expression;
using System.Configuration;

namespace SimplePMT
{
	[WebService(Namespace = "http://www.twentypints.com/AwishPMT")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.ComponentModel.ToolboxItem(false)]
	public class SvnLogService : System.Web.Services.WebService
	{
		private Task CreateTask(Member member, TaskInfo task)
		{
			Task dataTask;
			dataTask = new Task();
			dataTask.AssignedDateTime = DateTime.Now;
			dataTask.AssignedMemberID = member.ID;
			dataTask.ClosedDateTime = Convert.ToDateTime("1/1/1900 12:00:00 AM");
			dataTask.ComplexityEstimatedID = TaskComplexity.FindOne(Expression.Eq(TaskComplexity.Columns.Label, "Moderate")).ID;
			dataTask.CreateDateTime = DateTime.Now;
			dataTask.CreateMemberID = member.ID;
			dataTask.Description = task.Title;
			dataTask.DueDate = DateTime.Now.AddDays(14);
			dataTask.HoursEstimated = task.EstimatedHours;
			dataTask.IsAccepted = true;
			dataTask.IsOnHold = false;
			dataTask.IsResolved = false;
			dataTask.IsWorking = false;
			dataTask.Priority = Priority.FindOne(Expression.Eq(Priority.Columns.Label, "Normal"));
			dataTask.TaskType = TaskType.FindOne(Expression.Eq(TaskType.Columns.Label, "Code"));
			dataTask.Title = task.Title;
			dataTask.Save();
			return dataTask;
		}
		private void AddTaskToGeneralBucket(Task dataTask, string nameOfGeneralBucket)
		{
			TaskContainer container = TaskContainer.FindOne(Expression.Eq(TaskContainer.Columns.Label, nameOfGeneralBucket));
			TaskContainerItem item = new TaskContainerItem();
			item.TaskContainer = container;
			item.TaskID = dataTask.ID;
			item.Save();
		}
		private void CreateTaskNote(Member member, Task dataTask, TaskInfo task, bool timeAlreadyAccountedFor)
		{
			TaskNote note = new TaskNote();
			note.DateTime = DateTime.Now;
			if (!timeAlreadyAccountedFor)
			{
				note.Hours = task.TimeSpentOnTask;
			}
			note.IsSVNSource = true;
			note.Member = member;
			note.Notes = task.Message;
			note.Task = dataTask;
			note.Save();
		}
		private void CreateSvnCommit(CommitInfo info, Member member)
		{
			Task dataTask = null;
			CreateSvnCommit(info, member, dataTask);
		}
        private void CreateSvnCommit(CommitInfo info, Member member, Task dataTask)
		{
			SvnCommit commit = new SvnCommit();
			commit.Task = dataTask;
			commit.DateTime = DateTime.Now;
			commit.Log = info.Log;
			commit.Member = member;
			commit.Revision = info.Revision;
			commit.Repository = info.Repository;
			commit.Save();
		}
		[WebMethod]
		public SvnLogServiceResponse LogPostCommit(CommitInfo info)
		{
			SvnLogServiceResponse result = new SvnLogServiceResponse();
			result.Info = info;
			result.Success = true;
			using (TransactionScope scope = new TransactionScope())
			{
				try
				{
					string nameOfGeneralBucket = ConfigurationManager.AppSettings["GeneralBucket"];
					Member member = Member.FindOne(Expression.Eq("UserName", info.UserName));
					if (info.Tasks != null && info.Tasks.Length > 0)
					{
						bool timeAlreadyAccountedFor = false;
						foreach (TaskInfo taskInfo in info.Tasks)
						{
							
							Task task = Task.Find(taskInfo.TaskID);
							if (taskInfo.TaskID == 0 && !String.IsNullOrEmpty(taskInfo.Title))
							{
								task = CreateTask(member, taskInfo);
								AddTaskToGeneralBucket(task, nameOfGeneralBucket);
							}
							CreateTaskNote(member, task, taskInfo, timeAlreadyAccountedFor);
							CreateSvnCommit(info, member, task);
							timeAlreadyAccountedFor = true;
						}						
					}
					else
					{
						CreateSvnCommit(info, member);
					}

					scope.VoteCommit();
				}
				catch (ActiveRecordException ex)
				{
					result.Success = false;
					result.Message = ex.ToString();
					scope.VoteRollBack();
				}
			}
			return result;
		}
	}
}
